/**
 * Copyright (C) 2017 PatSnap Pte Ltd, All Rights Reserved.
 */
package com.job.conf;


import com.job.interfaces.CommentThriftService;
import com.job.service.CommentThriftServiceImpl;

import org.apache.thrift.server.TServer;
import org.apache.thrift.server.TThreadPoolServer;
import org.apache.thrift.transport.TServerSocket;
import org.apache.thrift.transport.TServerTransport;
import org.apache.thrift.transport.TTransportException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;


@Configuration
public class ThriftConfig {


    ExecutorService executor = Executors.newSingleThreadExecutor();

    @Autowired
    private CommentThriftServiceImpl commentThriftServiceImpl;

    @Bean
    public TServerTransport tServerTransport() {
        try {
            return new TServerSocket(7911);
        } catch (TTransportException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Bean
    public TServer tServer() {
        //发布服务
//        CommentThriftService.Processor processor = new CommentThriftService.Processor(
//                new CommentThriftServiceImpl());
        CommentThriftService.Processor processor = new CommentThriftService.Processor(commentThriftServiceImpl);
        TServer server = new TThreadPoolServer(new TThreadPoolServer.Args(
                tServerTransport()).processor(processor));
        return server;
    }

    @PostConstruct
    public void init() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                tServer().serve();
            }
        });
    }

}

